#include "Game.h"

Game::Game()
{
	system("cls");
	m_running = true;
	m_core = 0;
}

Game::~Game()
{
	shut();
}

void Game::init()
{
	//obtain instance of core framework
	m_core = clkCore::getInstance();

	//init the core framework
	m_core->init("ClockEngine",
		V2DF(CLK_SCREEN_HX-400.0f,CLK_SCREEN_HY-300.0f),
		V2DF(800,600),true,false);
	
	//set asset paths
	m_core->setFontPath("../Assets/Fonts/");
	m_core->setMeshPath("../Assets/Meshes/");
	m_core->setShaderPath("../Assets/Shaders/");
	m_core->setTexturePath("../Assets/Textures/");

	//create assets
	m_core->createMesh(&m_mesh,"mesh_cube.dae");
	m_core->createMesh(&m_mesh,"mesh_gum.dae");
	m_core->createShader(&m_shaderOrtho,"shader.hlsl","VShader_Orthogonal","PShader_Texture");
	m_core->createShader(&m_shaderColor,"shader.hlsl","VShader_Perspective","PShader_Color");
	m_core->createShader(&m_shaderLight,"phong.hlsl",0,0);

	//init camera object
	m_camera.init(
		CLK_CAMERA_FIRSTPERSON,
		m_core->getAppRDimension());
	m_camera.applyYaw(135.0f);
	m_camera.applyPitch(25.0f);
	m_camera.update();
	m_camera.applyPosition(m_camera.getLook()*-60.0f);
	m_core->setCamera(&m_camera);

	//reset timer
	m_core->resetTime();
}

void Game::shut()
{
	//shut down core framework
	m_core->shut();
}

void Game::loop()
{
	  //-------//
	 //**PRE**//
	//-------//

	//update timer
	m_core->updateTime();

	//update input
	m_core->acquire();

	  //----------//
	 //**UPDATE**//
	//----------//

	float timeD = m_core->getElapsed();
	V3DF mouseD = m_core->mouseDelta();

	//update camera
	float lspd = 500.0f*timeD;
	float cspd = 20.0f*timeD;
	m_camera.applyYaw(mouseD.x*lspd);
	m_camera.applyPitch(mouseD.y*lspd);
	if(m_core->keyInput(DIK_W))
		m_camera.applyPosition(m_camera.getLook()*cspd);
	else if(m_core->keyInput(DIK_S))
		m_camera.applyPosition(m_camera.getLook()*-cspd);
	if(m_core->keyInput(DIK_D))
		m_camera.applyPosition(m_camera.getRight()*cspd);
	else if(m_core->keyInput(DIK_A))
		m_camera.applyPosition(m_camera.getRight()*-cspd);
	m_camera.update();

	  //--------//
	 //**DRAW**//
	//--------//

	//clear draw area
	m_core->drawClear();

	//set shader and draw grid
	m_core->setShader(&m_shaderColor);
	drawGrid(V3DF(100,0,100),5,1);

	//set shader and draw mesh
	m_core->setShader(&m_shaderLight);
	m_core->drawMesh(&m_mesh,0,0,1.0f);

	//present draw area
	m_core->drawPresent();

	  //--------//
	 //**POST**//
	//--------//
}

bool Game::running()
{
	return (m_running && m_core->exit());
}
